#ifndef LEETCODE_CN_CPP_PROBLEMS_14_H
#define LEETCODE_CN_CPP_PROBLEMS_14_H

#include <limits>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

namespace problem_14
{

class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        if (strs.empty())
        {
            return {};
        }
        string prefix;
        size_t minSize = accumulate(
            strs.begin(), strs.end(),
            numeric_limits<size_t>::max(), [](size_t minSize, const string &str) {
                return min(minSize, str.size());
            });
        for (size_t i = 0; i < minSize; ++i)
        {
            bool ok = true;
            char c = strs.front()[i];
            for (const auto &str : strs)
            {
                if (str[i] != c)
                {
                    ok = false;
                    break;
                }
            }
            if (ok)
            {
                prefix += c;
            }
            else
            {
                break;
            }
        }
        return move(prefix);
    }
};

} // namespace problem_14

#endif //LEETCODE_CN_CPP_PROBLEMS_14_H
